package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.GoodsCategoryVO;
import com.cskaoyan.bean.vo.GoodsRelatedVO;
import com.cskaoyan.bean.vo.WxGoodsDetailVO;
import com.cskaoyan.bean.vo.WxGoodsListVO;
import com.cskaoyan.service.WxGoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author rebaogu
 * @description 1, 商品部分相关 + 2, 一个显示首页信息商品
 * @date 2022.04.08 21:56
 * @package com.cskaoyan.controller.wx
 * @Classname GoodsController
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {
    @Autowired
    WxGoodsService wxGoodsService;


    // todo
    // 在微信小窗口上显示有多少个商品
    @RequestMapping("count")
    public BaseRespVo goodsCount() {
        int count = wxGoodsService.goodsCount();
        return BaseRespVo.ok(count);
    }


    // 首页点击目录 进入二级目录
    @RequestMapping("category")
    public BaseRespVo goodsCategory(Integer id) {
        GoodsCategoryVO goodsCategoryVO = wxGoodsService.goodsCategory(id);

        return BaseRespVo.ok(goodsCategoryVO);
    }

    @RequestMapping("list")
    public BaseRespVo goodsList(BaseParam baseParam, Integer brandId, Integer categoryId, String keyword,boolean isHot,boolean isNew) {
        // 获取用户ID -->shiro
        // Subject subject = SecurityUtils.getSubject();
        // MarketUser user = (MarketUser) subject.getPrincipal();
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        WxGoodsListVO wxGoodsListVO = null;
        if (user != null && !"".equals(user)) {
            Integer userId = user.getId();
            wxGoodsListVO = wxGoodsService.goodsList(baseParam, brandId, categoryId, keyword, userId, isHot, isNew);
        } else {// 如果不登录的话 userId为-1 传入
             wxGoodsListVO = wxGoodsService.goodsList(baseParam, brandId, categoryId, keyword, -1, isHot, isNew);
        }
        return BaseRespVo.ok(wxGoodsListVO);
    }

    // 商品详细信息
    @RequestMapping("detail")
    public BaseRespVo goodsDetail(Integer id) {
        // 获取用户ID -->shiro
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();

        WxGoodsDetailVO wxGoodsDetailVO = wxGoodsService.goodsDetail(id, user);

        return BaseRespVo.ok(wxGoodsDetailVO);

    }

    // 商品详情页 相关商品 --> 查询这个 商品所属 分类里面的信息
    @RequestMapping("related")
    public BaseRespVo goodsRelated(Integer id) {
        GoodsRelatedVO goodsRelatedVO = wxGoodsService.goodsRelated(id);
        return BaseRespVo.ok(goodsRelatedVO);
    }
}

